package com.example.myapplication.util;

import android.graphics.Bitmap;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;

import java.io.File;
import java.io.FileOutputStream;

public class ImageUtil {
    public static String saveBitMap(Bitmap bitmap, String child) {
        //保存在本地 产品还没决定要不要保存在本地
        FileOutputStream fos;
        String path = getAppImageCache() + child;
        try {
            // SD卡根目录z
            File file = new File(path);
            fos = new FileOutputStream(file);
            //压缩图片 30 是压缩率，表示压缩70%; 如果不压缩是100，表示压缩率为0
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            fos.close();
            if (bitmap.isRecycled()) {
                bitmap.recycle();
            }
            return path;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getAppImageCache() {
        String path = PathUtils.getExternalAppDcimPath() + File.separator + "image" + File.separator;
        return isExistDir(path);
    }
    private static String isExistDir(String path) {
        FileUtils.createOrExistsDir(path);
        return path;
    }
}
